//
//  LCHotListTableViewCell.m
//  meiyinxiang
//
//  Created by apple on 2017/10/28.
//  Copyright © 2017年 hbjApple. All rights reserved.
//

#import "LCHotListTableViewCell.h"
#import "LCHotImageBtn.h"
#import "UIImage+Category.h"
#import "LCHotTopThreeBtn.h"
#import "LCHotTopOneBtn.h"
#import <POP.h>
#import <UIImageView+WebCache.h>
@interface LCHotListTableViewCell ()
@property (weak, nonatomic) IBOutlet UILabel *hotLabel;

@end
@implementation LCHotListTableViewCell

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
  //  [self addtopThreeBtns];
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}
+ (LCHotListTableViewCell *)loadNibHotlistCell{
    UINib *nib = [UINib nibWithNibName:@"LCHotListTableViewCell" bundle:nil] ;
    return [[nib instantiateWithOwner:nil options:nil] lastObject];
}
- (void)setTopModelArray:(NSArray *)topModelArray{
    _topModelArray = topModelArray;
    for (UIView *subview in self.contentView.subviews) {
        [subview removeFromSuperview];
    }
    [self addSubview:self.hotLabel];
    [self addHotListBtns];
}
#pragma mark -animationBtns
- (void)addtopThreeBtns{
    CGFloat secondAndThirdItemWith = 55;
    CGFloat secondAndThirdItemHeight = 82;
    CGFloat originalX = 40;
    CGFloat originalY = 28 - 200;
    LCHotTopThreeBtn *secondBtn = [LCHotTopThreeBtn loadNibWithLCHotBtn];
    secondBtn.frame = CGRectMake(originalX, originalY, secondAndThirdItemWith, secondAndThirdItemHeight);
    secondBtn.iconImageView.layer.cornerRadius = 28;
    secondBtn.iconImageView.layer.masksToBounds = YES;
    UIImage *plimge = [UIImage imageNamed:@"board_second and third"];
   TopHotestViewModel *secdModel = _topModelArray[1];
    secondBtn.iconImageView.contentMode = UIViewContentModeScaleAspectFill;
    [secondBtn.iconImageView sd_setImageWithURL:[NSURL URLWithString:secdModel.userface] placeholderImage:plimge completed:^(UIImage * _Nullable image, NSError * _Nullable error, SDImageCacheType cacheType, NSURL * _Nullable imageURL) {
        if (image) {
//             secondBtn.iconImageView.image = [UIImage hbj_AvatarIcon:image size:image.size backgroundColor:[UIColor whiteColor]];
            secondBtn.iconImageView.image = image;
        }else{
             secondBtn.iconImageView.image = [UIImage hbj_AvatarIcon:plimge size:plimge.size backgroundColor:[UIColor whiteColor]];
        }
       
    }];
 
    LCHotTopThreeBtn *thirdBtn = [LCHotTopThreeBtn loadNibWithLCHotBtn];
    secondBtn.crownImageView.image = [UIImage imageNamed:@"crown_second"];
    CGFloat X = ScreenWidth - originalX - secondAndThirdItemWith;
   // thirdBtn.iconImageView.image = [UIImage hbj_AvatarIcon:imge size:imge.size backgroundColor:[UIColor whiteColor]];
    TopHotestViewModel *thirdModel = _topModelArray[2];
    [thirdBtn.iconImageView sd_setImageWithURL:[NSURL URLWithString:thirdModel.userface] placeholderImage:plimge completed:^(UIImage * _Nullable image, NSError * _Nullable error, SDImageCacheType cacheType, NSURL * _Nullable imageURL) {
        if (image) {
//            thirdBtn.iconImageView.image = [UIImage hbj_AvatarIcon:image size:image.size backgroundColor:[UIColor whiteColor]];
            thirdBtn.iconImageView.image = image;
        }else{
            thirdBtn.iconImageView.image = [UIImage hbj_AvatarIcon:plimge size:plimge.size backgroundColor:[UIColor whiteColor]];
        }
        
    }];
    thirdBtn.frame = CGRectMake(X, originalY, secondAndThirdItemWith,
                                secondAndThirdItemHeight);
    thirdBtn.iconImageView.layer.cornerRadius = 28;
    thirdBtn.iconImageView.layer.masksToBounds = YES;
    thirdBtn.crownImageView.image =  [UIImage imageNamed:@"crown_third"];
    thirdBtn.iconImageView.contentMode = UIViewContentModeScaleAspectFill;
    LCHotTopOneBtn *topOneBtn = [LCHotTopOneBtn loadNibWithLCHotTopOneBtn];
    CGFloat topOneX = ScreenWidth/2 - 35;
    CGFloat topOneY = 10 - 200;
    CGFloat topOneWith = 70;
    CGFloat topOneHieght = 95;
    topOneBtn.frame = CGRectMake(topOneX, topOneY, topOneWith, topOneHieght);
    topOneBtn.iconImageView.layer.cornerRadius = 35;
    topOneBtn.iconImageView.layer.masksToBounds = YES;
 //   topOneBtn.iconImageView.image = [UIImage hbj_AvatarIcon:imge size:imge.size backgroundColor:[UIColor whiteColor]];
    TopHotestViewModel *oneModel = _topModelArray[0];
    [topOneBtn.iconImageView sd_setImageWithURL:[NSURL URLWithString:oneModel.userface] placeholderImage:plimge completed:^(UIImage * _Nullable image, NSError * _Nullable error, SDImageCacheType cacheType, NSURL * _Nullable imageURL) {
        if (image) {
//            topOneBtn.iconImageView.image = [UIImage hbj_AvatarIcon:image size:image.size backgroundColor:[UIColor whiteColor]];
            topOneBtn.iconImageView.image = image;
        }else{
            topOneBtn.iconImageView.image = [UIImage hbj_AvatarIcon:plimge size:plimge.size backgroundColor:[UIColor whiteColor]];
        }
        
    }];
    topOneBtn.iconImageView.contentMode = UIViewContentModeScaleAspectFill;
    [self.contentView addSubview:topOneBtn];
    [self.contentView addSubview:secondBtn];
    [self.contentView addSubview:thirdBtn];
    topOneBtn.hidden = YES;
    secondBtn.hidden = YES;
    thirdBtn.hidden = YES;
    [self addTopThreeAnimation:topOneBtn topTwo:secondBtn topThree:thirdBtn];

}
- (void)addHotListBtns{
    NSInteger hotListCount = _topModelArray.count - 3;
    CGFloat originalY = 115 - 200;
    CGFloat originalX = 15;
    CGFloat itemWith = 40;
    CGFloat blankSpace = (ScreenWidth - 2*originalX - itemWith*5)/4;
    for (NSInteger i = 0; i < hotListCount; i++) {
        LCHotImageBtn *hotBtn = [LCHotImageBtn loadNibWithLCHotBtn];
        UIImage *plimge = [UIImage imageNamed:@"board_second and third"];
        TopHotestViewModel *oneModel = _topModelArray[3+i];
        hotBtn.layer.cornerRadius = 20;
        hotBtn.layer.masksToBounds = YES;
        hotBtn.imgaView.contentMode = UIViewContentModeScaleAspectFill;
        [hotBtn.imgaView sd_setImageWithURL:[NSURL URLWithString:oneModel.userface] placeholderImage:plimge completed:^(UIImage * _Nullable image, NSError * _Nullable error, SDImageCacheType cacheType, NSURL * _Nullable imageURL) {
            if (image) {
//                hotBtn.imgaView.image = [UIImage hbj_AvatarIcon:image size:image.size backgroundColor:[UIColor whiteColor]];
                hotBtn.imgaView.image = image;
            }else{
                hotBtn.imgaView.image = [UIImage hbj_AvatarIcon:plimge size:plimge.size backgroundColor:[UIColor whiteColor]];
            }
            
        }];
       // hotBtn.imgaView.image = [UIImage hbj_AvatarIcon:imge size:imge.size backgroundColor:[UIColor whiteColor]];
        //行
        NSInteger line = i/5;
        //列
        NSInteger row = i%5;
        if (i > 5) {
            row = (i-5)%5;
        }
        CGFloat X = originalX + row*(itemWith + blankSpace);
        CGFloat Y = originalY +  line*(itemWith + 10) ;
        hotBtn.frame = CGRectMake(X, Y, itemWith, itemWith);
        hotBtn.hidden = YES;
        [self.contentView addSubview:hotBtn];
        [self addListAnimation:hotBtn i:i];
    }
    
}
#pragma mark - 添加动画
- (void)addTopThreeAnimation:(LCHotTopOneBtn *)topOneBtn topTwo:(LCHotTopThreeBtn*)topTwoBtn topThree:(LCHotTopThreeBtn *)topThreeBtn{
    topOneBtn.hidden = NO;
    topTwoBtn.hidden = NO;
    topThreeBtn.hidden = NO;
    NSInteger threeCase = _topModelArray.count - 3;
    NSInteger twoCase = _topModelArray.count - 2;
    NSInteger oneCase = _topModelArray.count - 1;
    for (NSInteger i = _topModelArray.count - 3; i < _topModelArray.count; i++) {
      POPSpringAnimation *animation  = [POPSpringAnimation animationWithPropertyNamed:kPOPLayerPositionY];
      
        //弹力系数
        animation.springBounciness = 8;
        //弹力速度，取值范围 0～20，数值越大，默认数值12
        animation.springSpeed = 8;
        //设置动画启动时间
        animation.beginTime = CACurrentMediaTime() + i * 0.05;
        if (i == threeCase) {
            //设置动画属性
            animation.fromValue = @(topThreeBtn.center.y);
            animation.toValue = @(topThreeBtn.center.y + 200);
            [topThreeBtn pop_addAnimation:animation forKey:nil];
        }else if (i == twoCase){
            animation.fromValue = @(topTwoBtn.center.y );
            animation.toValue = @(topTwoBtn.center.y + 200);
            [topTwoBtn pop_addAnimation:animation forKey:nil];
        }else if (i == oneCase){
            animation.fromValue = @(topOneBtn.center.y );
            animation.toValue = @(topOneBtn.center.y + 200);
            [topOneBtn pop_addAnimation:animation forKey:nil];
        }
//        switch (i) {
//            case threeCase:
//
//                break;
//            case twoCase:
//
//                break;
//            case oneCase:
//
//                break;
//
//            default:
//                break;
//        }
    }
}
- (void)addListAnimation:(LCHotImageBtn *)hotBtn i:(NSInteger )i {
    POPSpringAnimation *animation = [POPSpringAnimation animationWithPropertyNamed:kPOPLayerPositionY];
    //设置动画属性
    animation.fromValue = @(hotBtn.center.y);
    animation.toValue = @(hotBtn.center.y + 200);
    //弹力系数
    animation.springBounciness = 8;
    //弹力速度，取值范围 0～20，数值越大，默认数值12
    animation.springSpeed = 8;
    //设置动画启动时间
    animation.beginTime = CACurrentMediaTime() + i * 0.05;
    if (i == _topModelArray.count - 4) {
        __weak typeof(self) weakSelf = self;
        animation.completionBlock = ^(POPAnimation *anim, BOOL finished) {
            [weakSelf addtopThreeBtns];

        };
    }
    hotBtn.hidden = NO;
    //3 添加动画
    [hotBtn pop_addAnimation:animation forKey:nil];

}
@end
